package com.ycg.biquge.controller;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.ycg.biquge.common.Result;
import com.ycg.biquge.common.UserContext;
import com.ycg.biquge.mapper.BookCaseMapper;
import com.ycg.biquge.pojo.BookCase;
import com.ycg.biquge.pojo.Novel;
import com.ycg.biquge.service.BookCaseService;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

import java.awt.print.Book;
import java.util.List;

/**
 * @author YCG
 * @date 2024-04-29 21:05
 * @description 书架控制器
 */
@RestController
@RequestMapping("/bookcase")
public class BookCaseController {

    @Resource
    private BookCaseService bookCaseService;

    /**
     * 获取书架信息, 需要登录
     * @return 小说列表
     */
    @GetMapping
    public Result<List<BookCase>> getBookCase() {
        // 获取用户id
        Integer id = UserContext.getUser().getId();
        LambdaQueryWrapper<BookCase> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(BookCase::getUserId,id).orderByDesc(BookCase::getUpdateTime);
        List<BookCase> list = bookCaseService.list(wrapper);
        if (ObjectUtil.isNull(list)) return Result.ok(null);
        return Result.ok(list);
    }

    /**
     * 添加书架记录
     * @return ok
     */
    @PostMapping
    public Result<String> addBookCase(@RequestBody BookCase bookCase) {
        return bookCaseService.addBookcase(bookCase);
    }

    /**
     * 删除书架记录
     * @param id 书架id
     * @return ok
     */
    @DeleteMapping("/{id}")
    public Result<String> removeBookCase(@PathVariable String id) {
        bookCaseService.removeById(id);
        return Result.ok("删除成功");
    }

    @Resource
    private BookCaseMapper bookCaseMapper;

    /**
     * 修改书架记录
     * @return ok
     */
    @PutMapping()
    public Result<String> updateBookCase(@RequestBody BookCase bookCase) {
        Integer userId = UserContext.getUser().getId();
        System.out.println(bookCase);
        bookCaseMapper.updateNew(bookCase,userId);
        return Result.ok("修改成功");
    }
}
